/**
 * Created by gzf on 2015/9/17.
 */
var user_config = {
    openId:undefined,
    phone:undefined,
    type:undefined,
    timeTotal:5,
    timeTotal_bak:5,
    userid:''
}
/*伪类:active生效*/
//document.body.addEventListener('touchstart',function (){},false);
/**
 * login-register form and aside
 * */
var header = $('header').hammer();
header.on('tap',function(){
    if(!user_config.userid){
        //unlogin
        $('#lrModel').modal();
    }else{
        //login
        var inital_left = $('.user-info').css('left');
        $('.user-info').animate({left:0},'normal','linear');
        var back = $('.user-info .back-box').hammer();
        back.on('tap',function(){
            $('.user-info').animate({left:inital_left},'normal','linear');
            console.log(111);
        });
    }
});

function loginCheck(){
    if($('#tel').val() == null || $('#tel').val() == ''){
        showMessage('请输入您的手机号码');
        return false;
    }
    if(!/^1[34578]\d{9}$/.test($('#tel').val())) {
        showMessage('请输入正确的手机号码');
        return false;
    }
    if($('#password').val() == null || $('#password').val() == ''){
        showMessage('请输入密码');
        return false;
    }
    if(!/^$/.test($('#tel').val())) {
        showMessage('请输入正确的手机号码');
        return false;
    }
    return true;
}
//register form submit check
function registCheck(){
    if($('#tel-r').val() == null || $('#tel-r').val() == ''){
        showMessage('请输入您的手机号码');
        return false;
    }
    if(!/^1[34578]\d{9}$/.test($('#tel-r').val())) {
        showMessage('请输入正确的手机号码');
        return false;
    }
    if($('#check-code').val() == null || $('#check-code').val() == ''){
        showMessage('请输入验证码');
        return false;
    }
    if(!/^\d{6}$/.test($('#check-code').val())){
        showMessage('请输入正确的验证码');
        return false;
    }
    if($('#password-r').val() == null || $('#password-r').val() == ''){
        showMessage('请输入密码');
        return false;
    }
    return true;
}
function showMessage(message){
    var content = {};
    content.title = '';
    content.text = message;
    content.animation = 'slide-from-bottom';
    content.timer = 1000;
    content.showConfirmButton = false;
    swal(content);
}

var getCheckCode = $('.check-btn').hammer();
getCheckCode.on('click',function(){
    if($('.check-btn').hasClass('check-btn-tapped')) return;
    if($('#tel-r').val() == null || $('#tel-r').val() == ''){
        showMessage('请输入您的手机号码');
        return;
    }
    if(!/^1[34578]\d{9}$/.test($('#tel-r').val())) {
        showMessage('请输入正确的手机号码');
        return;
    }
    $(this).addClass('check-btn-tapped');
    setTimeout(timeSubr,1000);
    $.ajax({
        url:'getCheckCode.php',
        type:'POST',
        dataType:'text',
        data:{'tel':$("#tel-r").val()},
        error:function(data){
            console.log(data);
            showMessage(data);
        }
    });
});
function timeSubr(){
    user_config.timeTotal--;
    $('.check-btn').text('验证码' + user_config.timeTotal + "秒");
    if(user_config.timeTotal>=0){
        setTimeout(timeSubr,1000);
    }else{
        $('.check-btn').removeClass('check-btn-tapped').text('获取验证码');
        user_config.timeTotal = user_config.timeTotal_bak;
    }
}
var logoutBtn = $('.logout-btn').hammer();
logoutBtn.on('click',function(){
    swal({
        title:'',
        text:'您确认离开么？',
        confirmButtonColor:"#DD6B55",
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        showCancelButton:true,
        closeOnConfirm:false,
        closeOnCancel:false,
        showLoaderOnConfirm:true
    },function(e){
        if(e){
            $.ajax({
                url:'logout.php',
                type:'POST',
                dataType:'text',
                data:{'tel':$("#tel-r").val()},
                success:function(){
                    swal.close();
                },
                error:function(){
                    swal.close();
                    showMessage('退出失败，请重试');
                }
            });
        }else{
            swal.close();
        }
    });
    $('.sweet-alert').css({
        "width":"74%",
        "height":"140px",
        "left":"13%",
        "top":"40%"
    });
    $('.sweet-alert p').css('font-size','20px');
});

/*check Login or not*/
function checkIsLogin(){
    if(!user_config.userid){
        $('#lrModel').modal();
        return false;
    }
    return true;
}
function checkOnline(){
    if(navigator.onLine){
        return true;
       //showMessage('网络不可用');
    }
    return false;
}







